package com.apanal.qlife.personal.controller;

import java.text.ParseException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.org.model.Organization;
import com.apanal.qlife.org.service.OrganizationService;
import com.apanal.qlife.sys.model.User;

@Controller
@RequestMapping("/audit")
public class AuditCenterController {

	@Autowired
	private OrganizationService orgService;

	// 个人审核信息（未审核）
	@SystemLog(module = "个人中心审核管理", operation = "分页查询待审核信息", type = LogType.SELECT)
	@RequiresPermissions("mycenter:audit:view")
	@RequestMapping(value = "auditListNo")
	public String auditListNo(@CurrentUser User user, Model model,
			Organization org, Page page) {
		org.setOrgId(user.getOrgId());
		Organization orgz = orgService.findOrganizationById(org);
		String orgPIds = orgz.getOrgPIds() + orgz.getOrgId().toString();
		org.setOrgPIds(orgPIds);
		org.setAuditStatus(1);
		PageReturn<Organization> pr = orgService.findAudiListNo(org, page);
		model.addAttribute("result", pr);
		return "personal/auditListNo";
	}

	// 个人审核信息（已审核）
	@SystemLog(module = "个人中心审核管理", operation = "分页查询已审核信息", type = LogType.SELECT)
	@RequiresPermissions("mycenter:audit:view")
	@RequestMapping(value = "auditList")
	public String auditList(@CurrentUser User user, Model model,
			Organization org, Page page) {
		org.setOrgId(user.getOrgId());
		org.setAuditStatus(2);
		Organization orgz = orgService.findOrganizationById(org);
		String orgPIds = orgz.getOrgPIds() + orgz.getOrgId().toString();
		org.setOrgPIds(orgPIds);
		PageReturn<Organization> pr = orgService.findAudiList(org, page);
		model.addAttribute("result", pr);
		return "personal/auditList";
	}

	// 查看已审核信息详情
	@RequiresPermissions("mycenter:audit:deal")
	@RequestMapping(value = "/{orgId}/{orgType}/auditDtail")
	public String auditDtail(@CurrentUser User user, Model model,
			Organization org, Page page) {

		// 所属平台，所属渠道
		Organization orgz = orgService.findOrganization(org.getOrgId(),
				org.getOrgType());
		model.addAttribute("org", orgz);

		return "personal/auditDtail";
	}

	// 跳转到审核页面
	@RequiresPermissions("mycenter:audit:deal")
	@RequestMapping(value = "/{orgId}/{orgType}/audit")
	public String audit(@CurrentUser User user, Model model, Organization org,
			Page page) {
		// 所属平台，所属渠道
		Organization orgz = orgService.findOrganization(org.getOrgId(),
				org.getOrgType());
		model.addAttribute("org", orgz);
		return "personal/dealAudit";
	}

	/**
	 * 处理审核
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "个人中心审核管理", operation = "处理审核信息", type = LogType.UPDATE)
	@ResponseBody
	@RequestMapping(value = "dealAudit")
	public String saveAnnounce(@CurrentUser User user, Model model,
			Organization org) throws ParseException {
		Organization orgz = orgService.selectOrganizationById(org);// 当前记录
		orgService.updateOrg(orgz, user, org);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

}
